﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CMU.Certificates;

namespace CMU.Manage
{
    public partial class UCThoiGian : UserControl
    {
        CertificateEntities context = new CertificateEntities();
        public UCThoiGian()
        {
            InitializeComponent();
        }

        private void UCThoiGian_Load(object sender, EventArgs e)
        {
            var loadtg = from tg in context.ThoiGian
                         select tg;
            dataGridView1.DataSource = loadtg.ToList();
            dataGridView1.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ThoiGian tg = new ThoiGian();
            tg.NamHoc = textBox2.Text;
            tg.HocKy = textBox3.Text;
            
            context.ThoiGian.Add(tg);
            int count = context.SaveChanges();
            if (count > 0)
            {
                MessageBox.Show("Da them moi");
                UCThoiGian_Load(sender, e);
            }
            else
                MessageBox.Show("Da ton tai");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int tgID;
            if (Int32.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out tgID))
            {
                using (CertificateEntities newcontext = new CertificateEntities())
                {
                    ThoiGian tg = new ThoiGian() { Id = tgID };
                    newcontext.ThoiGian.Attach(tg);
                    //context.ObjectStateManger.ChangeObjectState(mh, EntityState.Deleted);
                    //newcontext.Entry(mh).State = System.Data.EntityState.Deleted;
                    newcontext.ThoiGian.Remove(tg);

                    int count = newcontext.SaveChanges();
                    if (count > 0)
                    {
                        MessageBox.Show("Da xoa");
                        UCThoiGian_Load(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("Mon Hoc da duoc lien ket");
                    }

                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int tgID;
            if (Int32.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out tgID))
            {
                using (CertificateEntities context = new CertificateEntities())
                {

                    ThoiGian thoigian = new ThoiGian() { Id = tgID };
                    thoigian = (from tg in context.ThoiGian
                                 where tg.Id == tgID
                                 select tg).FirstOrDefault<ThoiGian>();

                    textBox2.Text = thoigian.NamHoc;
                    textBox3.Text = thoigian.HocKy;


                }
            }
        }
    }
}
